package com.hspedu.furns.web;

import com.hspedu.furns.service.AdminService;
import com.hspedu.furns.service.impl.AdminServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * @author 徐
 * @version 1.0
 */
public class AdminServlet extends BasicServlet {
    //定义AdminService
    private AdminService adminService = new AdminServiceImpl();

    public void admin(HttpServletRequest request,HttpServletResponse response) throws ServletException, IOException {
        String username = request.getParameter("username");
        String password = request.getParameter("password");

        if (adminService.loginAdmin(username,password)) {
            //解决bug,如果member为null.管理员登陆也会被拦截，使其登录
            request.getSession().setAttribute("manageUrl",username);
            //重定向会被过滤拦截
            request.getRequestDispatcher("/views/manage/manage_menu.jsp").
                    forward(request,response);
        }else {
            //System.out.println("登陆失败");
            request.setAttribute("msg","用户名或密码错误");
            request.setAttribute("username",username);
            request.getRequestDispatcher("/views/manage/manage_login.jsp")
                    .forward(request,response);
        }
    }

    /**
     * 自己添加功能:退出管理员登陆
     * 清除ssesion的 "manageUrl",再次访问后被过滤器拦截
     * @param request
     * @param response
     * @throws ServletException
     * @throws IOException
     */
    public void quitManage(HttpServletRequest request,HttpServletResponse response) throws ServletException, IOException {
        //2.从session中删除
        request.getSession().removeAttribute("manageUrl");
        //重定向到首页
        response.sendRedirect(request.getContextPath()+"/index.jsp");

    }
}
